package com.qys.livesMall.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qys.livesMall.common.exception.QysException;
import com.qys.livesMall.framework.utils.SecurityUtils;
import com.qys.livesMall.system.enums.PlatformAdvertisementStateEnum;
import com.qys.livesMall.system.enums.SystemMessageEnum;
import com.qys.livesMall.system.model.dto.sys.PlatformAdvertisementDto;
import com.qys.livesMall.system.model.dto.sys.PlatformAdvertisementQueryPackageDto;
import com.qys.livesMall.system.model.dto.sys.PlatformAdvertisementStateDto;
import com.qys.livesMall.system.model.entity.H2PlatformAdvertisement;
import com.qys.livesMall.system.mapper.H2PlatformAdvertisementMapper;
import com.qys.livesMall.system.model.vo.sys.PlatformAdvertisementDetailVO;
import com.qys.livesMall.system.model.vo.sys.PlatformAdvertisementVo;
import com.qys.livesMall.system.service.H2PlatformAdvertisementDetailService;
import com.qys.livesMall.system.service.H2PlatformAdvertisementService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import jodd.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;

/**
 * <p>
 * 平台广告管理 服务实现类
 * </p>
 *
 * @author wzy
 * @since 2023-07-05
 */
@Service
public class H2PlatformAdvertisementServiceImpl extends ServiceImpl<H2PlatformAdvertisementMapper, H2PlatformAdvertisement> implements H2PlatformAdvertisementService {

    @Resource
    private H2PlatformAdvertisementDetailService h2PlatformAdvertisementDetailService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(PlatformAdvertisementDto platformAdvertisementDto) {
        H2PlatformAdvertisement h2PlatformAdvertisement=new H2PlatformAdvertisement();
        BeanUtil.copyProperties(platformAdvertisementDto,h2PlatformAdvertisement);
        h2PlatformAdvertisement.setCreateBy(SecurityUtils.getUsername());
        h2PlatformAdvertisement.setCreateTime(LocalDateTime.now());
        h2PlatformAdvertisement.setState(PlatformAdvertisementStateEnum.OFFLINE.getCode());
        save(h2PlatformAdvertisement);
        h2PlatformAdvertisementDetailService.add(platformAdvertisementDto.getPlatformAdvertisementDetailDtos(),h2PlatformAdvertisement.getId());
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(PlatformAdvertisementDto platformAdvertisementDto) {
        H2PlatformAdvertisement h2PlatformAdvertisement=getById(platformAdvertisementDto.getId());
        if (null == h2PlatformAdvertisement){
            throw new QysException(SystemMessageEnum.SYSTEM_NOT_ID);
        }
        h2PlatformAdvertisement.setName(platformAdvertisementDto.getName());
        h2PlatformAdvertisement.setPosition(platformAdvertisementDto.getPosition());
        h2PlatformAdvertisement.setUpdateBy(SecurityUtils.getUsername());
        h2PlatformAdvertisement.setUpdateTime(LocalDateTime.now());
        updateById(h2PlatformAdvertisement);
        h2PlatformAdvertisementDetailService.deletebyPid(h2PlatformAdvertisement.getId());
        h2PlatformAdvertisementDetailService.add(platformAdvertisementDto.getPlatformAdvertisementDetailDtos(),h2PlatformAdvertisement.getId());
        return true;
    }

    @Override
    public IPage<PlatformAdvertisementVo> query(PlatformAdvertisementQueryPackageDto dto) {
        LambdaQueryWrapper<H2PlatformAdvertisement> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(H2PlatformAdvertisement::getDeleted, 0);
        queryWrapper.like(StringUtil.isNotBlank(dto.getName()),H2PlatformAdvertisement::getName,dto.getName());
        queryWrapper.eq(null != dto.getPosition(),H2PlatformAdvertisement::getPosition,dto.getPosition());
        queryWrapper.eq(null!= dto.getState(),H2PlatformAdvertisement::getState,dto.getState());
        queryWrapper.ge(null!=dto.getStartTime(),H2PlatformAdvertisement::getCreateTime,dto.getStartTime());
        queryWrapper.le(null!=dto.getEndTime(),H2PlatformAdvertisement::getCreateTime,dto.getEndTime());
        queryWrapper.orderByDesc(H2PlatformAdvertisement::getCreateTime);
        IPage<PlatformAdvertisementVo> page=page(new Page<>(dto.getPageNum(), dto.getPageSize()), queryWrapper).convert(e -> {
            PlatformAdvertisementVo platformAdvertisementVo=new PlatformAdvertisementVo();
            BeanUtils.copyProperties(e,platformAdvertisementVo);
            platformAdvertisementVo.setStateStr(PlatformAdvertisementStateEnum.getEnum(platformAdvertisementVo.getState()).getName());
            platformAdvertisementVo.setCreatelongtime(e.getCreateTime().toInstant(ZoneOffset.ofHours(8)).toEpochMilli());
            return platformAdvertisementVo;
        });
        return page;
    }

    @Override
    public PlatformAdvertisementVo info(Long id) {
        H2PlatformAdvertisement h2PlatformAdvertisement=getById(id);
        PlatformAdvertisementVo platformAdvertisementVo=new PlatformAdvertisementVo();
        BeanUtils.copyProperties(h2PlatformAdvertisement,platformAdvertisementVo);
        List<PlatformAdvertisementDetailVO> h2PlatformAdvertisementDetails = h2PlatformAdvertisementDetailService.getByPId(id);
        platformAdvertisementVo.setPlatformAdvertisementDetailVOS(h2PlatformAdvertisementDetails);
        return platformAdvertisementVo;
    }

    @Override
    public boolean state(PlatformAdvertisementStateDto dto) {
        H2PlatformAdvertisement h2PlatformAdvertisement=getById(dto.getId());
        if (null == h2PlatformAdvertisement){
            throw new QysException(SystemMessageEnum.SYSTEM_NOT_ID);
        }
        if (h2PlatformAdvertisement.getPosition().intValue()==0){
            LambdaUpdateWrapper<H2PlatformAdvertisement> updateWrapper=new LambdaUpdateWrapper<>();
            updateWrapper.eq(H2PlatformAdvertisement::getDeleted, 0);
            updateWrapper.eq(H2PlatformAdvertisement::getPosition,0);
            updateWrapper.set(H2PlatformAdvertisement::getState,1);
            update(updateWrapper);
            h2PlatformAdvertisement.setState(0);
        }else {
            h2PlatformAdvertisement.setState(dto.getState());
        }
        return updateById(h2PlatformAdvertisement);
    }

    @Override
    public PlatformAdvertisementVo queryHomeInfo() {
        LambdaQueryWrapper<H2PlatformAdvertisement> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(H2PlatformAdvertisement::getDeleted, 0);
        queryWrapper.eq(H2PlatformAdvertisement::getPosition,0);
        queryWrapper.eq(H2PlatformAdvertisement::getState,0);
        List<H2PlatformAdvertisement> h2PlatformAdvertisements= list(queryWrapper);
        if (CollectionUtils.isNotEmpty(h2PlatformAdvertisements)){
            PlatformAdvertisementVo platformAdvertisementVo=new PlatformAdvertisementVo();
            BeanUtils.copyProperties(h2PlatformAdvertisements.get(0),platformAdvertisementVo);
            List<PlatformAdvertisementDetailVO> h2PlatformAdvertisementDetails = h2PlatformAdvertisementDetailService.getByPId(platformAdvertisementVo.getId());
            platformAdvertisementVo.setPlatformAdvertisementDetailVOS(h2PlatformAdvertisementDetails);
            return platformAdvertisementVo;
        }
        return null;
    }
}
